package com.onetech.adrewards.free.dialogs;

import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.analytics.tracking.android.Fields;
import com.google.analytics.tracking.android.MapBuilder;
import com.google.analytics.tracking.android.Tracker;
import com.onetech.adrewards.free.R;
import com.onetech.adrewards.free.fragments.ListGiftCardFragment.GiftCarFragmentInterface;
import com.zuzu.dialogs.BaseDialog;

public class MessageDialog extends BaseDialog {
	private TextView mTxtMessage = null;

	private Button mBtnYes = null;
	private Button mBtnNo = null;

	private int mTypeMessage = 0;
	private int mType = 0;
	private Tracker mTracker;
	private Activity mActivity = null;
	private Bundle mParams = null;
	private GiftCarFragmentInterface mGiftCarFragmentInterface;

	public MessageDialog(Activity context, int typeMessage, Bundle params,
			String title, GiftCarFragmentInterface _mGiftCarFragmentInterface) {
		super(context, title, TYPE_NO_BUTTON, R.layout.dialog_message_layout);
		mGiftCarFragmentInterface = _mGiftCarFragmentInterface;
		this.mParams = params;
		mActivity = context;
		mTxtMessage = (TextView) findViewById(R.id.txtMessage);
		mBtnYes = (Button) findViewById(R.id.btnYes);
		mBtnNo = (Button) findViewById(R.id.btnNo);
		mTypeMessage = typeMessage;
		if (typeMessage == 1) // not enough point
		{
			String msg = context
					.getString(R.string.exchange_message_not_enough_point);
			int discount = Integer.parseInt(params.getString("discount"));

			msg = msg.replace("[exchange_coin]", String.valueOf(discount));
			mTxtMessage.setText(msg);
			mBtnYes.setText(R.string.get_coin_now_label);
			mBtnNo.setText(R.string.cancel_exchange);
		}
		if (typeMessage == 2) // exchange confirm
		{

			int current_coin = Integer.parseInt(params
					.getString("current_coin"));
			int discount = Integer.parseInt(params.getString("discount"));
			String cardName = params.getString("card_name");
			this.mType = Integer.parseInt(params.getString("type"));
			if (current_coin < discount) {
				dismiss();
			}
			String msg = context
					.getString(R.string.exchange_message_confrim_exchange);
			int after_coin = current_coin - discount;
			msg = msg.replace("[current_coin]", String.valueOf(current_coin));
			msg = msg.replace("[after_coin]", String.valueOf(after_coin));
			msg = msg.replace("[exchange_coin]", String.valueOf(discount));
			msg = msg.replace("[card_name]", cardName);
			mTxtMessage.setText(Html.fromHtml(msg));
			mTxtMessage.setGravity(Gravity.LEFT);
			mBtnYes.setText(R.string.confirm_exchange);
			mBtnNo.setText(R.string.cancel_exchange);

		}
		initListener();

	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.btnYes:
			if (mTypeMessage == 1) {
				//((HomeActivity) mActivity).switchCurrentItem(1);
				mGiftCarFragmentInterface.setCurrentItem(1);
				this.cancel();
			}
			if (mTypeMessage == 2) {
				ExchangeFinishedDialog exchange = new ExchangeFinishedDialog(
						mActivity, this.mType);
				exchange.show();
				dismiss();
			}
			break;
		case R.id.btnNo:
			dismiss();
			break;
		default:
			super.onClick(v);
			break;
		}
	}

	private void initListener() {

		mBtnYes.setOnClickListener(this);
		mBtnNo.setOnClickListener(this);

	}

	@Override
	public void onStart() {
		super.onStart();
		try {
			mTracker.set(Fields.SCREEN_NAME,
					mActivity.getString(R.string.ExchangeFragment));
			mTracker.send(MapBuilder.createAppView().build());
		} catch (Exception e) {
		}
	}
}
